#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 038

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.numbers.decimal_base import integer_to_digits, \
                                    isone_to_nine_pandigital, join_integers

def get_answer():
    """Question:
    
    Take the number 192 and multiply it by each of 1, 2, and 3:

    192 × 1 = 192
    192 × 2 = 384
    192 × 3 = 576
    
    By concatenating each product we get the 1 to 9 pandigital, 192384576. 
    We will call 192384576 the concatenated product of 192 and (1,2,3)
    
    The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 
    4, and 5, giving the pandigital, 918273645, which is the concatenated 
    product of 9 and (1,2,3,4,5).
    
    What is the largest 1 to 9 pandigital 9-digit number that can be formed as 
    the concatenated product of an integer with (1,2, ... , n) where n > 1?
    """
    
    def nine_digit_product(k):    
        """Returns an integer formed from the concatenated products of
        [k] by 1 .. n.
        
        Note:
            - This function returns the value once it equals or exceeds
            9 digits. 
        """
        
        def inner_nine_digit_product(digits, multiple): 
            """Returns an integer formed from concatenating [digits] and the 
            product of k and [multiple]. If the length of [digits] equals or
            exceeds 9, it is returned instead.
            """
                
            if(len(digits) >= 9):
                return digits
             
            new_digits = digits + list(integer_to_digits(k * multiple))
            
            return inner_nine_digit_product(new_digits, multiple + 1)
            
        return join_integers(inner_nine_digit_product([], 1))
    
    #Return result.
    return max(
              filter(
                     isone_to_nine_pandigital,
                     (nine_digit_product(n) for n in xrange(1, 99999))
                    )
            )
      
if __name__ == "__main__":
    cProfile.run("print(get_answer())")
